﻿config.initpage = function(formNode){
	var event = formNode.event;
	
	//检查子表数据
	event.on('initother', function(event) {
		var form = event.form;
		var define = event.define;
		var pkvalue = form.get(define.pkcol);
        
		JxUtil.delay(1000, function(){
            var items = formNode.page.items.items;
            for (var i = 0; i < items.length; i++) {
                var data = items[i].data;
                if ('store_indet' == data) {
                    if (Ext.isEmpty(pkvalue)) {
                        //JxHint.alert('['+define.nodeid+']没有主键值，不能加载子表数据！');
                        return;
                    }
                    var subgrid = items[i].getComponent(0);//子表表格对象
                    var store = subgrid.getStore();
                    //如果没有加载数据则重新加载一次
                    if (store.getCount() == 0) {
                        Jxstar.loadSubData(subgrid, pkvalue);
                        console.log('---------------loadSubData.pkvalue'+pkvalue);
                    }
                }
            }
        });
	});
	
};

config.eventcfg={
	copycreate : function(){
		var self = this;
        var form = self.page.getForm();//表单对象
        var myGrid = form.myGrid;//主表表格对象
        var keyid = 'jxstar-986-5101';//主键
        
		//在表格中添加一条记录
		var insertData = function(record) {
			var store = myGrid.getStore();
			store.insert(0, record);
			myGrid.getSelectionModel().selectFirstRow();
		};
      
		//加载显示数据
		var options = {
			where_sql: 'store_in.in_id = ?',
			where_type: 'string',
			where_value: keyid,
			callback: function(data) { 
				//如果没有数据则执行新增
				if (data.length == 0) {
				  self.page.formNode.event.create();
				} else {
				 var r = self.page.formNode.event.newRecord(data[0]);
				 self.page.getForm().myRecord = r;
				 self.page.getForm().loadRecord(r);
				 //不刷新表格，直接在表格中新增一条记录
				 insertData(r);
				}
			}
		};
		Jxstar.queryData(self.define.nodeid, options);
        
 		//重新加载子表数据
		    var items = self.page.items.items;
		    for(var i=0; i<items.length; i++){
			   var data = items[i].data
               if ('store_indet' == data || 'store_indet1' == data) {
				  var subgrid = items[i].getComponent(0);//子表表格对象
                  Jxstar.loadSubData(subgrid, keyid);
               }
		    }
	}
};